﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Happy.Web.Mvc.Paging
{
    /// <summary>
    /// 分页栏视图，例如：【第一页】【1】【2】【3】【4】【下一页】。
    /// </summary>
    public sealed class PageBarView : IEnumerable<PageBarView.Page>
    {
        private long _start;
        private long _end;

        /// <summary>
        /// 构造方法。
        /// </summary>
        public PageBarView(
            long pageNumberLength,
            long itemsPerPage,
            long totalItems,
            long totalPages,
            long currentPage)
        {
            this.PageNumberLength = pageNumberLength;
            this.ItemsPerPage = itemsPerPage;
            this.TotalItems = totalItems;
            this.TotalPages = totalPages;
            this.CurrentPage = currentPage;

            _start = this.CurrentPage - (long)(Math.Floor(this.PageNumberLength / 2.0));
            if (_start < 1)
            {
                _start = 1;
            }

            _end = _start + this.PageNumberLength - 1;
            if (_end > this.TotalPages)
            {
                _end = this.TotalPages;
            }
        }

        /// <summary>
        /// 显示的页码数。
        /// </summary>
        public long PageNumberLength { get; private set; }

        /// <summary>
        /// 每页记录数。
        /// </summary>
        public long ItemsPerPage { get; private set; }

        /// <summary>
        /// 总记录数。
        /// </summary>
        public long TotalItems { get; private set; }

        /// <summary>
        /// 总页数。
        /// </summary>
        public long TotalPages { get; private set; }

        /// <summary>
        /// 当前页。
        /// </summary>
        public long CurrentPage { get; private set; }

        /// <summary>
        /// 是否有上一页。
        /// </summary>
        public bool HasPrevPage()
        {
            return _start > 1;
        }

        /// <summary>
        /// 上一页。
        /// </summary>
        public long PrevPage()
        {
            return _start - 1;
        }

        /// <summary>
        /// 是否有下一页。
        /// </summary>
        public bool HasNextPage()
        {
            return _end < this.TotalPages;
        }

        /// <summary>
        /// 下一页。
        /// </summary>
        public long NextPage()
        {
            return _end + 1;
        }

        /// <inheritdoc />
        public IEnumerator<Page> GetEnumerator()
        {
            for (var i = _start; i <= _end; i++)
            {
                yield return new Page
                {
                    Number = i,
                    IsActive = i == this.CurrentPage
                };
            }
        }

        /// <inheritdoc />
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        /// <summary>
        /// 页码。
        /// </summary>
        public sealed class Page
        {
            /// <summary>
            /// 页码，从 1 开始编码。
            /// </summary>
            public long Number { get; internal set; }

            /// <summary>
            /// 是否是活动。
            /// </summary>
            public bool IsActive { get; internal set; }
        }
    }
}
